
from get_month_quarter import *
from get_tasks_develop_report_detail import *

db = get_db_connection()

#meta = MetaData(db)
#db.metadata.reflect()

def run():

#    first_day,last_day = get_week()
#    first_day,last_day,month_view = get_month()
#    report_type = '2'
    first_day,last_day,quarter_view,quarter = get_quarter()
    report_type = '3'
    
    quarter = long(quarter)
    if(quarter <> 3) and (quarter <> 6) and (quarter <> 9) and (quarter <> 12):
        sys.exit()
    
#    report_type = '1'
    data =  get_tasks_develop_report_detail(first_day,last_day,report_type)

    #print last_day,first_day

    req_model = get_model('tasks_develop_report_detail')
    req_ass = get_model('assignment')
    
    req_model.remove(and_(req_model.c.report_date == last_day,req_model.c.report_type == report_type))
    for v,t in data.items():
        #print 't',t,'v',v
        r = req_model(**t)
        r.save()


#    sql_ass = req_ass.filter(and_(req_ass.c.stage != 'FN'))
#    for row in sql_ass:
#        ass_delay = req_model.filter(and_(req_model.c.ass != row.id,
#                              req_model.c.report_date == last_day, 
#                              req_model.c.t_delay == 1))
#        delay_flag = 0
#        for row1 in ass_delay:
#            delay_flag = 1
#            if row.status != '2':
#                req_ass.table.update(req_ass.c.id ==row.id).execute(status = '2')
#        if delay_flag == 0 and row.status == '2':
#            req_ass.table.update(req_ass.c.id ==row.id).execute(status = '1')

if __name__ == '__main__':
    try:
        db.begin()
        run()
        db.commit()
        log.info("finish")
    except Exception, e:
        db.rollback()
        log.exception(e)
        log.error('error')
        raise
    
#def run_delay_flag():
#
##    first_day,last_day = get_week()
#    first_day,last_day,month_view = get_month()
#    report_type = '2'
#    
##    report_type = '1'
##    data =  get_tasks_develop_report_detail(first_day,last_day,report_type)
#
#    #print last_day,first_day
#
#    req_model = get_model('tasks_develop_report_detail')
#    req_ass = get_model('assignment')
#    
#    
#    sql_ass = req_ass.filter(and_(req_ass.c.stage != 'FN'))
#    for row in sql_ass:
#        ass_delay = req_model.filter(and_(req_model.c.assignment == row.id,
#                              req_model.c.report_date == last_day,
#                              req_model.c.report_type == report_type,
#                              req_model.c.t_delay == 1))
#        delay_flag = 0
#        for row1 in ass_delay:
#            delay_flag = 1
#            if row.status != '2' and row.status != '3' and row.status != '4' and row.status != '5':
#                req_ass.table.update(req_ass.c.id ==row.id).execute(status = '2')
#            break
#        if delay_flag == 0 and row.status == '2':
#            req_ass.table.update(req_ass.c.id ==row.id).execute(status = '1')
#        if row.end_date < last_day and row.status != '2' and row.stage != 'FN' and row.stage != '16' and row.status != '4' and row.status != '5':
#            req_ass.table.update(req_ass.c.id ==row.id).execute(status = '2')
#
#if __name__ == '__main__':
#    try:
#        db.begin()
#        run_delay_flag()
#        db.commit()
#        log.info("finish")
#    except Exception, e:
#        db.rollback()
#        log.exception(e)
#        log.error('error')
#        raise
